package io.renren.modules.blog.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.utils.Result;
import io.renren.common.page.PageData;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.blog.dto.BlogTagDto;
import io.renren.modules.blog.service.BlogTagService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;

import java.util.Map;

/**
 * (BlogTagEntity)表控制层
 *
 * @author chy
 * @since 2024-06-20 17:33:30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blogTag")
@Api(tags = "")
public class BlogTagController {
    /**
     * 服务对象
     */
    private final BlogTagService blogTagService;

    /**
     * 分页查询
     *
     * @param params 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public Result<PageData<BlogTagDto>> queryByPage(@RequestParam Map<String, Object> params) {
        return new Result<PageData<BlogTagDto>>().ok(blogTagService.page(params));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("详情")
    @LogOperation("详情")
    @GetMapping("{id}")
    public Result<BlogTagDto> get(@PathVariable("id") Long id) {
        BlogTagDto data = blogTagService.get(id);
        return new Result<BlogTagDto>().ok(data);
    }

    /**
     * 新增数据
     *
     * @param blogTagDto 实体
     */
    @ApiOperation("保存")
    @LogOperation("保存")
    @PostMapping
    public Result save(@RequestBody BlogTagDto blogTagDto) {
        //效验数据
        ValidatorUtils.validateEntity(blogTagDto, AddGroup.class, DefaultGroup.class);

        blogTagService.save(blogTagDto);

        return new Result();
    }

    /**
     * 编辑数据
     *
     * @param blogTagDto 实体
     */

    @ApiOperation("修改")
    @LogOperation("修改")
    @PutMapping
    public Result update(@RequestBody BlogTagDto blogTagDto) {
        //效验数据
        ValidatorUtils.validateEntity(blogTagDto, UpdateGroup.class, DefaultGroup.class);

        blogTagService.update(blogTagDto);

        return new Result();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除")
    @LogOperation("删除")
    @DeleteMapping("{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Long id) {
        return new Result<Boolean>().ok(blogTagService.deleteById(id));
    }

}

